package com.admin.kotlintest.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.admin.kotlintest.R
import com.admin.kotlintest.utils.LocationUtils
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.CameraUpdate
import com.amap.api.maps2d.CameraUpdateFactory
import com.amap.api.maps2d.model.*
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.geocoder.GeocodeResult
import com.amap.api.services.geocoder.GeocodeSearch
import com.amap.api.services.geocoder.RegeocodeResult
import com.socks.library.KLog
import kotlinx.android.synthetic.main.activity_map.*
import com.amap.api.services.geocoder.RegeocodeQuery
import kotlinx.android.synthetic.main.layout_title.*


/**
 * Created by 吴昶 on 2018/4/19.
 */
class MapActivity:AppCompatActivity(),AMapLocationListener,AMap.OnCameraChangeListener,GeocodeSearch.OnGeocodeSearchListener{

    var latitude:Double=0.0
    var marker:Marker?=null
    var longitude:Double=0.0
    var aMap:AMap?=null
    var geocodeSearch:GeocodeSearch?=null
    var address:String=""

    override fun onLocationChanged(p0: AMapLocation?) {
        KLog.d(p0!!.address)
        latitude=p0.latitude
        longitude=p0.longitude
        var l=LatLng(latitude, longitude)
        marker=aMap!!.addMarker(MarkerOptions().position(l).title("当前位置").snippet("DefaultMarker"))
        address=p0.address
        tv_map_address.text=p0.address
        aMap!!.moveCamera(CameraUpdateFactory.changeLatLng(l))
        aMap!!.moveCamera(CameraUpdateFactory.zoomBy(12f))
        LocationUtils.stopLocation()
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.statusBarColor= resources.getColor(R.color.title)
        window.navigationBarColor= resources.getColor(R.color.title)
        setContentView(R.layout.activity_map)
        map_view.onCreate(savedInstanceState)
        initData()
    }

    fun initData(){
        aMap=map_view.map
        LocationUtils.getCurrentLocation(this,this)
        aMap!!.setOnCameraChangeListener(this)
        geocodeSearch= GeocodeSearch(this)
        geocodeSearch!!.setOnGeocodeSearchListener(this)
        linear_title_right.visibility=View.VISIBLE
        tv_title_right.visibility= View.VISIBLE
        tv_title_right.text="确定"
        tv_title.text="选择上车地点"
        //定位当前位置
//        var locationStyle=MyLocationStyle()
//        locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE)
//        aMap!!.setMyLocationStyle(locationStyle)
//        aMap!!.isMyLocationEnabled=true

        tv_title_right.setOnClickListener {
            var intent=Intent()
            intent.putExtra("lat",latitude)
            intent.putExtra("lon",longitude)
            intent.putExtra("address",address)
            setResult(Activity.RESULT_OK,intent)
            finish()
        }
    }

    override fun onGeocodeSearched(p0: GeocodeResult?, p1: Int) {

    }

    override fun onRegeocodeSearched(p0: RegeocodeResult?, p1: Int) {
        address=p0!!.regeocodeAddress.formatAddress
        tv_map_address.text=p0!!.regeocodeAddress.formatAddress
    }

    override fun onCameraChangeFinish(p0: CameraPosition?) {
        var latlon=p0!!.target
        marker?.let {
            aMap!!.clear()
        }
        latitude=latlon.latitude
        longitude=latlon.longitude
        var l=LatLonPoint(latlon.latitude,latlon.longitude)
        val query = RegeocodeQuery(l, 200f, GeocodeSearch.AMAP)
        geocodeSearch!!.getFromLocationAsyn(query)
    }
    override fun onCameraChange(p0: CameraPosition?) {

    }

    override fun onResume() {
        super.onResume()
        map_view.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        map_view.onDestroy()

    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        map_view.onSaveInstanceState(outState)
    }

}